package com.xc.ezplan.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.xc.ezplan.admin.entity.AdminUser;
import com.xc.ezplan.admin.service.impl.AdminUserServiceImpl;
import com.xc.ezplan.admin.vo.AdminUserVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.dto.AdminUserDTO;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author lixingchen
 * @date 2022/11/15
 * @description 后台用户信息
 */
@RestController
@RequestMapping("/user")
@Api(tags = "后台用户")
@RequiredArgsConstructor
public class AdminUserController {

	private final AdminUserServiceImpl adminUserService;

	@Log("新增用户")
	@ApiOperation("新增用户")
	@PostMapping
	@SaCheckPermission("user.admin.add")
	public EzResult<Object> createUser(@Validated @RequestBody AdminUserVo adminUserVo) {
		return EzResult.ok(adminUserService.createUser(adminUserVo));
	}

	@Log("删除用户")
	@ApiOperation("删除用户")
	@DeleteMapping
	@SaCheckPermission("user.admin.delete")
	public EzResult<Object> deleteUser(@RequestBody List<Long> ids) {
		if (ids.contains(1L)){
			return EzResult.failed("不能删除管理员账号");
		}
		adminUserService.deleteUser(ids);
		return EzResult.ok();
	}

	@Log("修改用户")
	@ApiOperation("修改用户")
	@PutMapping
	@SaCheckPermission("user.admin.update")
	public EzResult<Object> updateUser(@Validated @RequestBody AdminUserVo adminUserVo) {
		adminUserService.updateUser(adminUserVo);
		return EzResult.ok();
	}

	@ApiOperation("查询用户")
	@GetMapping
	public EzResult<Object> queryUser(@RequestParam(required = false) Long id) {
		return EzResult.ok(adminUserService.queryUser(id));
	}


	@ApiOperation("按条件分页查询用户")
	@PostMapping("/page")
	public EzResult<Object> pageQueryUser(@RequestBody BaseSearchBody<AdminUser> baseSearchBody) {
		return EzResult.ok(adminUserService.pageQueryUser(baseSearchBody.getSearchCondition(), baseSearchBody.getPage()));
	}

	@ApiOperation("通过用户名查询用户")
	@GetMapping("/username")
	public EzResult<AdminUserDTO> queryUserByUserName(
			@RequestParam(value = "username", required = false) String username) {
		return EzResult.ok(adminUserService.queryUserByUserName(username));
	}

	@ApiOperation("登录时获取用户信息")
	@GetMapping("/info")
	public EzResult<Map<String, Object>> queryUserInfo() {
		return EzResult.ok(adminUserService.queryUserInfo());
	}

	@Log("修改密码")
	@ApiOperation("修改密码")
	@PutMapping("/updatePassword")
	@SaCheckPermission("user.admin.reset")
	public EzResult<Object> updateUserPass(@RequestBody JSONObject jsonObject) {
		adminUserService.updateUserPass(jsonObject);
		return EzResult.ok();
	}

	@Log("修改状态")
	@ApiOperation("修改状态")
	@PutMapping("/changeEnabled")
	@SaCheckPermission("user.admin.valid")
	public EzResult<Object> changeEnabled(@RequestParam Long ids,@RequestParam Boolean enabled) {
		return EzResult.ok(adminUserService.changeEnabled(ids,enabled));
	}


	@ApiOperation("导出用户数据")
	@PostMapping(value = "/export")
	public void exportUser(@RequestBody List<Long> ids) throws Exception {
		adminUserService.exportUser(ids);
	}

	@Log("修改头像")
	@ApiOperation("修改头像")
	@PostMapping(value = "/updateAvatar")
	public EzResult<Object> updateUserAvatar() {
		return EzResult.ok(adminUserService.updateUserAvatar());
	}

	@Log("修改邮箱")
	@ApiOperation("修改邮箱")
	@PostMapping(value = "/updateEmail/{code}")
	public EzResult<Object> updateUserEmail() {
		return EzResult.ok(adminUserService.updateUserEmail());
	}

	@Log("修改用户：个人中心")
	@ApiOperation("修改用户：个人中心")
	@PutMapping(value = "center")
	public EzResult<Object> centerUser() {
		return EzResult.ok(adminUserService.centerUser());
	}
}
